package cn.iocoder.yudao.module.alipay.service.cpssource;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.alipay.controller.admin.cpssource.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.cpssource.CpsSourceDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.alipay.convert.cpssource.CpsSourceConvert;
import cn.iocoder.yudao.module.alipay.dal.mysql.cpssource.CpsSourceMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.alipay.enums.ErrorCodeConstants.*;

/**
 * 渠道标识 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CpsSourceServiceImpl implements CpsSourceService {

    @Resource
    private CpsSourceMapper cpsSourceMapper;

    @Override
    public Long createCpsSource(CpsSourceCreateReqVO createReqVO) {
        // 插入
        CpsSourceDO cpsSource = CpsSourceConvert.INSTANCE.convert(createReqVO);
        cpsSourceMapper.insert(cpsSource);
        // 返回
        return cpsSource.getId();
    }

    @Override
    public void updateCpsSource(CpsSourceUpdateReqVO updateReqVO) {
        // 校验存在
        validateCpsSourceExists(updateReqVO.getId());
        // 更新
        CpsSourceDO updateObj = CpsSourceConvert.INSTANCE.convert(updateReqVO);
        cpsSourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteCpsSource(Long id) {
        // 校验存在
        validateCpsSourceExists(id);
        // 删除
        cpsSourceMapper.deleteById(id);
    }

    private void validateCpsSourceExists(Long id) {
        if (cpsSourceMapper.selectById(id) == null) {
            throw exception(CPS_SOURCE_NOT_EXISTS);
        }
    }

    @Override
    public CpsSourceDO getCpsSource(Long id) {
        return cpsSourceMapper.selectById(id);
    }

    @Override
    public List<CpsSourceDO> getCpsSourceList(Collection<Long> ids) {
        return cpsSourceMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CpsSourceDO> getCpsSourcePage(CpsSourcePageReqVO pageReqVO) {
        return cpsSourceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CpsSourceDO> getCpsSourceList(CpsSourceExportReqVO exportReqVO) {
        return cpsSourceMapper.selectList(exportReqVO);
    }

    @Override
    public List<CpsSourceDO> getListByUserId(String userId) {
        return cpsSourceMapper.getListByUserId(userId);
    }

}
